import { toNumber } from "@/utils/index.js";

// 计算人民币价格
export function calculateSellingPriceRMB(sellingPrice, exchangeRate) {
  // if (sellingPrice <= 0 || exchangeRate <= 0) {
  //   throw new Error("Invalid input for selling price or exchange rate.");
  // }
  return (sellingPrice * exchangeRate).toFixed(2);
};

// 计算所有费用的总和
export function calculateTotalCosts(
  purchaseCost,
  headFreight,
  tailFreight,
  platformFee,
  otherFee,
  other_fee_currency,
  exchangeRate,
  sellingPriceRMB,
  vat_tax,
  tariff,
  oversea_into_cost,
  fba_cost,
) {
  const costs = [
    toNumber(purchaseCost, 0),
    toNumber(headFreight, 0),
    toNumber(tailFreight, 0),
    toNumber(platformFee, 0),
    toNumber(
      other_fee_currency === "%"
        ? sellingPriceRMB * (toNumber(otherFee, 0) / 100)
        : otherFee * exchangeRate,
      0
    ),
    toNumber(sellingPriceRMB * (toNumber(vat_tax, 0) / 100)),
    toNumber(tariff),
    toNumber(oversea_into_cost),
    toNumber(fba_cost * (toNumber(exchangeRate, 0))),

  ].reduce((acc, cur) => acc + cur, 0);
  return costs.toFixed(2);
};

// 计算VAT费用
export function calculateVATFee(row) {
  const selling_price_rmb = (row.selling_price_rmb =
    (Number(row.selling_price) * Number(row.exchange_rate)).toFixed(2) || 0);
  const vat_tax = row.vat_tax || 0;

  // 根据业务逻辑判断是否允许负数
  if (Number(selling_price_rmb) < 0 || Number(vat_tax) < 0) {
    return 0; // 或者根据业务需求返回其他值
  }
  return ((Number(selling_price_rmb) * Number(vat_tax)) / 100).toFixed(2);
};

// 计算产品毛利率
export function calculateProductGross(row) {
  let marginRatio = 0;
  if (row.selling_price && row.exchange_rate) {
    try {
      const sellingPriceRMB = calculateSellingPriceRMB(
        toNumber(row.selling_price),
        toNumber(row.exchange_rate)
      );
      const totalCosts = calculateTotalCosts(
        row.purchase_cost,
        row.head_freight,
        row.tail_freight,
        row.platform_fee,
        row.other_fee,
        row.other_fee_currency,
        row.exchange_rate,
        sellingPriceRMB,
        row.vat_tax,
        row.tariff,
        row.oversea_into_cost,
        row.fba_cost,
      );
      const numerator =
        toNumber(row.selling_price) * toNumber(row.exchange_rate) - totalCosts;

      marginRatio =
        numerator / (toNumber(row.selling_price) * toNumber(row.exchange_rate));
    } catch (error) {
      console.error("Failed to calculate estimated product gross margin:", error);
      return "0";
    }
  }
  const total = ((marginRatio || 0) * 100).toFixed(2);
  row.estimated_product_gross_margin = total;
  return total;
};

// 计算销售毛利率
export function calculateSaleGross(row) {
  let tate = 0;
  // if (row.estimated_product_gross_margin > 0) {
    tate =
      toNumber(row.estimated_product_gross_margin) -
      toNumber(row?.advertising_rate) -
      toNumber(row?.refund_rate);
  // }
  const total = tate.toFixed(2)
  row.estimated_sale_gross_margin = total;
  return total;
};

// 计算兑人民币
export const calculateAgainstRMB = (price, rate) => {
  const selling_price = price || 0;
  const exchange_rate = rate || 0;
  if (!selling_price || !exchange_rate) {
    return "--";
  }

  const total = (selling_price * exchange_rate).toFixed(2);
  return total;
};

// 计算备货金额
export const calculateStockTotal = (row) => {
  const suggestPrepareCount = Number(row.suggest_prepare_count || 0);
  const purchaseCost = Number(row.purchase_cost || 0);

  // 根据业务逻辑判断是否允许负数
  if (suggestPrepareCount < 0 || purchaseCost < 0) {
    return 0; // 或者根据业务需求返回其他值
  }

  return (suggestPrepareCount * purchaseCost).toFixed(2);
};